from flask import redirect, render_template
from flask import request, url_for
from flask_user import current_user, login_required, roles_accepted
from tank import db
from .. import bp
from ..forms import UserProfileForm


@bp.route('/')
def home_page():
    return render_template('user/home_page.html')


@bp.route('/user')
@login_required
def user_page():
    return render_template('user/user_page.html')


@bp.route('/admin')
@roles_accepted('admin')
def admin_page():
    return render_template('user/admin_page.html')


@bp.route('/profile', methods=['GET', 'POST'])
@login_required
def profile_page():
    form = UserProfileForm(request.form, current_user)
    if request.method == 'POST' and form.validate():
        form.populate_obj(current_user)
        db.session.commit()
        return redirect(url_for('user.home_page'))
    return render_template('user/user_profile_page.html', form=form)


from . import resources
